﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MiddleTier;

namespace ThreeTierDemo
{
    public delegate void DataLoadingComplete(String eitthvad);
    
    public partial class Form1 : Form
    {
        public DataLoadingComplete DataLoadingComplete = delegate { };

        public Form1()
        {
            InitializeComponent();
        }

        public void Load()
        {
            // Vírum upp databinding aftur útaf singleton..
            // Sjá kóða í InitializeComponent. this.studentBs.DataSource = typeof(MiddleTier.Facade);            
            this.studentBs.DataSource = Facade.getInstance;
            this.studentBs.DataMember = "Students";

            this.classBs.DataSource = Facade.getInstance;
            this.classBs.DataMember = "Classes";

            this.teacherBs.DataSource = Facade.getInstance;
            this.teacherBs.DataMember = "Teachers";

            this.dataGridView2.DoubleClick += this.foobar2;
            this.dataGridView2.CellValueChanged += this.foobar1;

            //this.DataLoadingComplete.Invoke("eitthvað sniðugt");
        }

        private void foobar1(object sender, EventArgs e)
        {
        }

        private void foobar2(object o, EventArgs ea)
        {
        }

        private void studentBs_AddingNew(object sender, AddingNewEventArgs e)
        {
            
        }

        private void studentBs_CurrentChanged(object sender, EventArgs e)
        {
            var bs = (BindingSource)sender;
        }

        private void dataGridView2_SelectionChanged(object sender, EventArgs e)
        {
            var c = (Class)this.classBs.Current;

            // Breytum kennurum eftir vali á námskeiði
            var query = from p in MiddleTier.Facade.getInstance.Teachers
                                        where p.Classes.Exists((x) => x.ClassNumber == c.ClassNumber)
                                        select p;
            this.teacherBs.DataSource = query.ToList();

            // Breytum stúdentum eftir vali á námskeiði
            var query2 = from s in MiddleTier.Facade.getInstance.Students
                    where s.Classes.Exists((x) => x.ClassNumber == c.ClassNumber)
                    select s;
            this.studentBs.DataSource = query2.ToList();            
        }

    }
}
